using System;
using Godot;
using KludgeBox;
using KludgeBox.Events;
using KludgeBox.Events.Global;
using VectorSurvivors.Multiplayer;
namespace VectorSurvivors;


/// <summary>
/// Глобальная точка доступа к данным и состоянию игры.
/// </summary>
public partial class Game : Node2D
{
	public static GameState StateContainer => _instance._gameState;
	public static GameStorage Storages => _instance._gameStorage;
	public static Curtain Curtain => _instance._curtain;
	
	[Export] [NotNull] private GameState _gameState { get; set;}
	[Export] [NotNull] private GameStorage _gameStorage { get; set;}
	[Export] [NotNull] private Curtain _curtain { get; set;}


	private static Game _instance;
	private readonly ServiceRegistry _registry = new();

	private static readonly EventPublisher<ReceivedPacketEvent> _packetPublisher =
		EventBus.GetEventPublisher<ReceivedPacketEvent>(true);
	public Game()
	{
		_instance = this;
		_registry.RegisterServices();
		EventBus.RegisterListeners(_registry);
	}

	public static void ChangeState(PackedScene scene, Action<Node> callback = null)
	{
		Curtain.ChangeState(scene, callback);
	}
	
	// Вызывается ДО этого же метода у детей
	/// <inheritdoc />
	public override void _EnterTree()
	{
		NotNullChecker.CheckProperties(this);
	}

	// Вызывается ПОСЛЕ этого же метода у детей
	/// <inheritdoc />
	public override void _Ready()
	{
		
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	/// <inheritdoc />
	public override void _PhysicsProcess(double delta)
	{
		
	}

	/// <inheritdoc />
	public override void _Input(InputEvent @event)
	{
		
	}

	/// <inheritdoc />
	public override void _Notification(int what)
	{
		if (what == NotificationPredelete)
		{
			CancelFree();
		}
	}
}